package leetcode

//https://leetcode.com/problems/container-with-most-water/


fun main(args: Array<String>) {
    println(ContainerWithMostWater().maxArea(intArrayOf(1, 2, 3, 4, 5, 6, 17, 18, 19)))
}

class ContainerWithMostWater {

    fun maxArea(height: IntArray): Int {
        var i = 0
        var j = height.size - 1
        var leftHeight: Int
        var rightHeight: Int
        var maxArea = 0
        while (i < j) {
            leftHeight = height[i]
            rightHeight = height[j]
            maxArea = Math.max(maxArea, Math.min(rightHeight, leftHeight) * (j - i))
            if (leftHeight < rightHeight)
                while (i < j && height[i] <= leftHeight)
                    i++
            else
                while (i < j && height[j] <= rightHeight)
                    j--
        }
        return maxArea
    }
}